package com.fayuan.nocv.TXApi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fayuan.nocv.entity.ChinaTotal;
import com.fayuan.nocv.entity.NocvData;
import com.fayuan.nocv.service.ChinaTotalService;
import com.fayuan.nocv.service.IndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ChinaTotalScript {

    @Autowired
    private ChinaTotalService chinaTotalService;
    @Autowired
    private IndexService indexService;

    @Scheduled(fixedDelay = 3600000)
    public void updateChinaTotalToDB() throws ParseException {
        HttpUtils httpUtils = new HttpUtils();
        String Alldata = null;
        try {
            Alldata = httpUtils.getData();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //所有数据
        JSONObject jsonObject = JSONObject.parseObject(Alldata);
        Object o = jsonObject.get("data");
//        System.out.println("data="+o);
        //获取整体疫情数据
        JSONObject jsonData = JSONObject.parseObject(o.toString());
        Object data = jsonData.get("chinaTotal");
//        System.out.println("chinaTotal"+data);
        //整体中国数据
        JSONObject jsonChina = JSONObject.parseObject(data.toString());
        Object china = jsonChina.get("total");
//        System.out.println("total"+china);

        JSONObject json = JSONObject.parseObject(china.toString());
        Object confirm = json.get("confirm");
        Object input = json.get("input");
        Object severe = json.get("severe");
        Object heal = json.get("heal");
        Object dead = json.get("dead");
        Object suspect = json.get("suspect");

        ChinaTotal chinaTotal = new ChinaTotal();
        chinaTotal.setConfirm((Integer) confirm);

        chinaTotal.setInput((Integer) input);
        chinaTotal.setSevere((Integer) severe);
        chinaTotal.setHeal((Integer) heal);
        chinaTotal.setDead((Integer) dead);
        chinaTotal.setSuspect((Integer) suspect);

        chinaTotal.setUpdateTime(new Date());
        boolean b = chinaTotalService.saveOrUpdate(chinaTotal);
        if (b){
            log.info("插入数据成功！");
        }


        JSONArray areaTree = jsonData.getJSONArray("areaTree");
        Object[] objects = areaTree.toArray();
        //遍历所有国家数据
//        for (Object object : objects) {
//            JSONObject object1 = JSONObject.parseObject(object.toString());
//
//            System.out.println( object1.get("name"));
//        }
        List<NocvData>NocvList = new ArrayList<>();
        //获取中国各省份疫情数据
        JSONObject chinaObjectJson = JSONObject.parseObject(objects[2].toString());
        JSONArray children = chinaObjectJson.getJSONArray("children");
        Object[] objects1 = children.toArray();
        //遍历中国各省份疫情数据
        for (Object o1 : objects1) {
            JSONObject jsonObject1 = JSONObject.parseObject(o1.toString());
            //获取省份名称
            Object name = jsonObject1.get("name");
            //获取省份数据（确诊，治愈，疑似，死亡）
            Object total = jsonObject1.get("total");
            JSONObject totaljson = JSONObject.parseObject(total.toString());
            //获取省份累计确诊数据
            Object confirm1 = totaljson.get("confirm");
            //获取省份治愈人数
            Object heal1 = totaljson.get("heal");
            //获取省份死亡人数
            Object dead1 = totaljson.get("dead");

            //将object转换为integer
            Integer confirmData = Integer.valueOf(confirm1.toString());
            Integer healData = Integer.valueOf(heal1.toString());
            Integer deadData = Integer.valueOf(dead1.toString());

            //将省份名称和确诊数据填充到集合
            NocvData nocvData = new NocvData();
            nocvData.setName(String.valueOf(name));
            //现有确诊=累计确诊-治愈人数-死亡人数
            Integer currConfirm = confirmData-healData- deadData;
            if (currConfirm<0){currConfirm=0;}
            nocvData.setValue(currConfirm);
            nocvData.setUpdateTime(new Date());
            NocvList.add(nocvData);
        }
        indexService.remove(new QueryWrapper<>());
        //将数据填充到mysql数据库
        boolean b1 = indexService.saveBatch(NocvList);
        if (b1){
            log.info("中国疫情地图数据更新成功！");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.flushDB();
    }
}
